package com.amikomgamedev.panjatpinang.database;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/*
 * AndEngineGamePanjatPinang
 * ScoreDb.java
 * Pratama Wijaya
 * set.mnemonix@gmail.com
 * 11:12:35 PM , Aug 7, 2012
 */

public class ScoreDbHelper extends SQLiteOpenHelper {

	private static final String	DBNAME				= "dbdbdb";
	private static final String	SCORE_TABLE_NAME	= "scoreTable";

	private ScoreDbHelper		myDbHelper;
	private SQLiteDatabase		myDb;
	private final Context		myContext;

	public ScoreDbHelper(Context context) {
		super(context, DBNAME, null, 2);
		this.myContext = context;
	}

	private void openConnection(Context con) {
		myDbHelper = new ScoreDbHelper(con);
		myDb = myDbHelper.getWritableDatabase();
		myDb = myDbHelper.getReadableDatabase();
	}

	public ScoreDbHelper CreateTable() throws SQLException {
		openConnection(myContext);
		myDb.execSQL("CREATE TABLE IF NOT EXISTS " + SCORE_TABLE_NAME
				+ "(score INTEGER, " + "nama VARCHAR(20)" + ")");
		myDb.close();
		return this;

	}

	public boolean isHighScore(float score) {
		openConnection(myContext);

		Cursor cur = myDb.rawQuery("SELECT score FROM " + SCORE_TABLE_NAME
				+ " " + "WHERE score < " + score, null);

		while (cur.moveToNext()) {
			myDb.close();
			return false;
		}
		myDb.close();
		return true;
	}

	public boolean isContent() {
		openConnection(myContext);
		Cursor cur = myDb.rawQuery("SELECT score FROM " + SCORE_TABLE_NAME,
				null);

		while (cur.moveToNext()) {
			myDb.close();
			return true;
		}
		myDb.close();
		return false;
	}

	public String getLatestScore() {
		openConnection(myContext);

		Cursor cur = myDb.rawQuery("SELECT score FROM " + SCORE_TABLE_NAME,
				null);
		while (cur.moveToNext()) {
			myDb.close();
			return cur.getString(0);
		}
		myDb.close();
		return "0";
	}

	public String[] getHighScore() {
		// openConnection(myContext);
		myDbHelper = new ScoreDbHelper(myContext);
		myDb = myDbHelper.getReadableDatabase();
		String[] data = null;

		Cursor cur = myDb.rawQuery("SELECT TOP 1 FROM " + SCORE_TABLE_NAME
				+ "ORDER BY score ASC", null);

		while (cur.moveToNext()) {
			data[0] = cur.getString(0);
			data[1] = cur.getString(1);
			myDb.close();
			return data;
		}
		myDb.close();
		return null;
	}

	public String getLatestName() {
		openConnection(myContext);
		Cursor cur = myDb
				.rawQuery("SELECT nama FROM " + SCORE_TABLE_NAME, null);
		while (cur.moveToNext()) {
			myDb.close();
			return cur.getString(0);
		}
		myDb.close();
		return "0";
	}

	public ScoreDbHelper insertNama(String nama) {
		if (isContent()) {
			openConnection(myContext);
			SQLiteStatement sqlInsert;
			String sql = "UPDATE " + SCORE_TABLE_NAME + " SET nama='" + nama
					+ "'";
			sqlInsert = myDb.compileStatement(sql);
			sqlInsert.executeInsert();
			myDb.close();
		} else {
			openConnection(myContext);
			SQLiteStatement sqlInsert;
			String sql = "INSERT INTO " + SCORE_TABLE_NAME
					+ " (nama) VALUES ('" + nama + "')";
			sqlInsert = myDb.compileStatement(sql);
			sqlInsert.executeInsert();
			myDb.close();

		}
		return this;
	}

	public ScoreDbHelper insertScore(float score) {
		if (isContent()) {
			openConnection(myContext);
			SQLiteStatement sqlInsert;
			String sql = "UPDATE " + SCORE_TABLE_NAME + " SET score='" + score
					+ "'";
			sqlInsert = myDb.compileStatement(sql);
			sqlInsert.executeInsert();
			myDb.close();
		} else {
			openConnection(myContext);
			SQLiteStatement sqlInsert;
			String sql = "INSERT INTO " + SCORE_TABLE_NAME
					+ " (score) VALUES ('" + score + "')";
			sqlInsert = myDb.compileStatement(sql);
			sqlInsert.executeInsert();
			myDb.close();

		}
		return this;

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}
